package com.xy.webadmin.app.config;

import com.xy.webadmin.app.constant.MqttConstants;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.messaging.MessageChannel;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;

/**
 * Spring Integration MQTT连接配置类
 *
 * @author xiaoyue
 * @date 2025-07-03
 */
@Slf4j
@Configuration
@IntegrationComponentScan
public class MqttConnectionConfig {
    /**
     * 创建MQTT客户端工厂的默认配置模板
     * 实际连接时会根据特定配置覆盖这些默认值
     *
     * @return MQTT客户端工厂
     */
    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        
        // 默认连接选项，可根据需要调整
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        options.setKeepAliveInterval(MqttConstants.ConnectionDefaults.KEEP_ALIVE_INTERVAL);
        options.setConnectionTimeout(MqttConstants.ConnectionDefaults.CONNECTION_TIMEOUT);
        options.setAutomaticReconnect(true);
        options.setMaxReconnectDelay(MqttConstants.ConnectionDefaults.MAX_RECONNECT_DELAY);
        
        // 设置默认遗嘱消息
        options.setWill(
            MqttConstants.ConnectionDefaults.DEFAULT_WILL_TOPIC, 
            MqttConstants.ConnectionDefaults.DEFAULT_WILL_MESSAGE.getBytes(), 
            MqttConstants.QosLevel.AT_LEAST_ONCE, 
            false
        );
        
        // 设置最大并发消息数
        options.setMaxInflight(MqttConstants.ConnectionDefaults.MAX_INFLIGHT);
        
        factory.setConnectionOptions(options);
        return factory;
    }
    
    /**
     * 消息转换器
     *
     * @return 默认消息转换器
     */
    @Bean
    public DefaultPahoMessageConverter messageConverter() {
        DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
        converter.setPayloadAsBytes(false);
        return converter;
    }
    
    /**
     * 输入消息通道，用于接收MQTT消息
     *
     * @return 消息通道
     */
    @Bean
    public MessageChannel mqttInputChannel() {
        return new DirectChannel();
    }
    
    /**
     * 输出消息通道，用于发送MQTT消息
     *
     * @return 消息通道
     */
    @Bean
    public MessageChannel mqttOutputChannel() {
        return new DirectChannel();
    }
    
    /**
     * 默认的MQTT消息发送处理器
     * 用于在没有特定配置的情况下发送消息
     *
     * @return 消息处理器
     */
    @Bean
    @ServiceActivator(inputChannel = "mqttOutputChannel")
    public MqttPahoMessageHandler defaultMqttOutboundHandler() {
        // 创建默认的消息处理器，使用随机客户端ID
        String clientId = MqttConstants.ConnectionDefaults.CLIENT_ID_PREFIX + System.currentTimeMillis();
        MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory());
        messageHandler.setAsync(true);
        messageHandler.setDefaultQos(MqttConstants.QosLevel.DEFAULT);
        messageHandler.setDefaultTopic(MqttConstants.SpringIntegration.DEFAULT_TOPIC);
        messageHandler.setConverter(messageConverter());
        return messageHandler;
    }
} 